﻿// -----------------------------------------------------------------------
// <copyright file="CampfireTests.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace DotNet37Signals.Client.Test.Bootstrappers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using DotNet37Signals.Client.Campfire;
    using DotNet37Signals.Client.Campfire.Contracts;
    using DotNet37Signals.Client.Campfire.Models;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CampfireTests
    {
        public void Run()
        {
            Console.WriteLine("----------------");
            Console.WriteLine(" Campfire Tests ");
            Console.WriteLine("----------------");

            this.Initialize();

            this.Account();
            //this.Rooms();
            this.Search();
            this.Messages();
            this.Transcripts();
            this.Users();

            Console.WriteLine("");
        }

        public CampfireClient Client { get; set; }

        public void Initialize()
        {
            var username = ConfigurationManager.AppSettings["username.campfire"];
            var password = ConfigurationManager.AppSettings["password"];
            var company = ConfigurationManager.AppSettings["company"];
            var keepAliveEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings["keepAliveEnabled"]);

            var credentials = new Credentials(username, password, company, keepAliveEnabled);
            this.Client = new CampfireClient(credentials);
        }

        public void Account()
        {
            Console.WriteLine();
            Console.WriteLine("Account Contract Tests");
            Console.WriteLine("**********************");

            var service = this.Client.Account;

            var account = service.Get();

            Console.WriteLine("Account Name: {0}", account.Name);
        }

        public void Rooms()
        {
            Console.WriteLine();
            Console.WriteLine("Rooms Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Rooms;

            var rooms = service.GetList();

            Console.WriteLine("Room Count: {0}", rooms.Count);

            if (rooms.Count > 0)
            {
                var room = rooms.First();
                
                room = service.Get(room.Id.ToString());

                Console.WriteLine("Room Name: {0}", room.Name);

                service.Join(room.Id.ToString());

                var presence = service.GetPresence();

                Console.WriteLine("Has Joined: {0}", presence.Any(p => p.Id == room.Id));

                service.Leave(room.Id.ToString());

                presence = service.GetPresence();

                Console.WriteLine("Has Left: {0}", !presence.Any(p => p.Id == room.Id));

                room.Name = "Test Room " + DateTime.Now.Ticks.ToString();

                service.Update(room.Id.ToString(), room);

                room = service.Get(room.Id.ToString());

                Console.WriteLine("Room Name: {0}", room.Name);

                var messages = service.GetRecentMessages(room.Id.ToString());

                Console.WriteLine("Message Count: {0}", messages.Count());

                var uploads = service.GetUploads(room.Id.ToString());

                Console.WriteLine("Uploads Count: {0}", uploads.Count());
            }

        }

        public void Search()
        {
            Console.WriteLine();
            Console.WriteLine("Search Contract Tests");
            Console.WriteLine("*********************");

            var service = this.Client.Search;

            var results = service.Get("Hello World");

            Console.WriteLine("Search Count: {0}", results.Count());
        }

        public void Messages()
        {
            Console.WriteLine();
            Console.WriteLine("Messages Contract Tests");
            Console.WriteLine("***********************");

            var roomService = this.Client.Rooms;
            var service = this.Client.Messages;

            var rooms = roomService.GetList();
            var room = rooms.First();

            var newText = service.TextMessage(room.Id, "Hello World");

            Console.WriteLine("Text Message: {0}", newText.Body);

            service.Highlight(newText.Id.ToString());

            service.RemoveHighlight(newText.Id.ToString());
        }

        public void Transcripts()
        {
            Console.WriteLine();
            Console.WriteLine("Transcripts Contract Tests");
            Console.WriteLine("**************************");

            var roomService = this.Client.Rooms;
            var service = this.Client.Transcripts;

            var rooms = roomService.GetList();
            var room = rooms.First();

            var trans = service.GetToday(room.Id.ToString());

            Console.WriteLine("Transcript Message Count: {0}", trans.Count());
        }

        public void Users()
        {
            Console.WriteLine();
            Console.WriteLine("Users Contract Tests");
            Console.WriteLine("********************");

            var service = this.Client.Users;

            var me = service.Me();

            Console.WriteLine("My Name: {0}", me.Name);

            var i = service.Get(me.Id.ToString());

            Console.WriteLine("My Name Again: {0}", i.Name);
        }
    }
}
